<template>
  <el-form ref="formRef" :model="form" label-width="280px" status-icon>
    <el-form-item label="是否开启" prop="open">
      <el-radio-group v-model="form.open">
        <el-radio :label="0">关闭</el-radio>
        <el-radio :label="1">开启</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="AppID" prop="app_id">
      <el-input v-model="form.app_id" clearable style="width: 240px"/>
    </el-form-item>
    <el-form-item label="private_key" prop="private_key">
      <el-input type="textarea" :rows=3 v-model="form.private_key" clearable style="width: 240px"/>
    </el-form-item>
    <el-form-item label="public_key" prop="public_key">
      <el-input type="textarea" :rows=3 v-model="form.public_key" clearable style="width: 240px"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" plain v-debounce="handleSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getConfig } from "@/api/system/config";

// 定义参数的类型
interface IConfigProps {
  configKey?: string;
  name?: string;
}

// 子组件接收父组件的值
// withDefaults：设置默认值  defineProps：接收父组件的参数
const props = withDefaults(defineProps<IConfigProps>(), {
  name: "",
  configKey: ""
});

onMounted(() => {
  getConfig({ key: props.configKey }).then(res => {
    if (Object.keys(res.data).length > 0) {
      form.value = res.data;
    }
  });
});

// 当前组件获取父组件传递的事件方法
const emits = defineEmits(["handleSubmit"]);
// 弹框的确定事件
const handleSubmit = () => {
  emits("handleSubmit", form.value);
};

let form = ref<any>({
  open: 0,
  private_key: "",
  public_key: "",
  app_id: ""
});
</script>

<style lang="scss" scoped></style>
